﻿#coding=utf-8
#Boa:Dialog:DlgOrderState

import wx

from product import CDataOrderState
from MyListCtrl import MySimpleListCtrl

def create(parent):
    return DlgOrderState(parent)

[wxID_DLGORDERSTATE, wxID_DLGORDERSTATEBTNDEL, wxID_DLGORDERSTATEBTNEDIT, 
 wxID_DLGORDERSTATEBTNNEW, 
] = [wx.NewId() for _init_ctrls in range(4)]

class DlgOrderState(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGORDERSTATE, name=u'DlgOrderState',
              parent=prnt, pos=wx.Point(555, 223), size=wx.Size(518, 444),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u8ba2\u5355\u72b6\u6001')
        self.SetClientSize(wx.Size(510, 410))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Center(wx.BOTH)

        self.btnNew = wx.Button(id=wxID_DLGORDERSTATEBTNNEW,
              label=u'\u65b0\u5efa', name=u'btnNew', parent=self,
              pos=wx.Point(8, 0), size=wx.Size(93, 26), style=0)
        self.btnNew.Bind(wx.EVT_BUTTON, self.OnBtnNewButton,
              id=wxID_DLGORDERSTATEBTNNEW)

        self.btnEdit = wx.Button(id=wxID_DLGORDERSTATEBTNEDIT,
              label=u'\u7f16\u8f91', name=u'btnEdit', parent=self,
              pos=wx.Point(104, 0), size=wx.Size(93, 26), style=0)
        self.btnEdit.Show(False)
        self.btnEdit.Bind(wx.EVT_BUTTON, self.OnBtnEditButton,
              id=wxID_DLGORDERSTATEBTNEDIT)

        self.btnDel = wx.Button(id=wxID_DLGORDERSTATEBTNDEL,
              label=u'\u5220\u9664', name=u'btnDel', parent=self,
              pos=wx.Point(200, 0), size=wx.Size(93, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGORDERSTATEBTNDEL)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        cols = [(CDataOrderState.fi_id, wx.LIST_FORMAT_LEFT, -1),
                (CDataOrderState.fi_name, wx.LIST_FORMAT_LEFT, 200),]
        self.lc = MySimpleListCtrl.factory(self, CDataOrderState, cols)
        self.lc.Move(wx.Point(8, 32))
        self.lc.SetSize(wx.Size(496, 368))
        self.lc.Bind(wx.EVT_LEFT_DCLICK, self.OnDClick)
        self.lc.refresh()

    def getSelVal(self):
        return self.lc.getData(self.selRow, CDataOrderState.fi_name)
        
    def OnBtnNewButton(self, event):
    
        dlg = wx.TextEntryDialog(self, u'输入新建订单状态：', u'新建订单状态', 
                            '', style=wx.OK|wx.CANCEL)
        if (dlg.ShowModal() == wx.ID_OK):
            CDataOrderState.add(dlg.GetValue())
            self.lc.refresh()

    def OnBtnEditButton(self, event):
        event.Skip()

    def OnBtnDelButton(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        id = self.lc.getData(item, CDataOrderState.fi_id)
        try:
            id = int(id)
        except ValueError as error:
            print ('error id:', id)
            return
        
        obj = CDataOrderState.findObj(id)
        obj.delete()
        self.lc.refresh()    
        
    def OnDClick(self, event):
        item = self.lc.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        self.selRow = item
        self.EndModal(wx.ID_OK)
